<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2019/2/4
 * Time: 2:35 AM
 */

namespace backend\models;

/**
 * Class PartTimeJob
 * @package backend\models
 */
class PartTimeJob extends \common\models\PartTimeJob
{


    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
//            [['uid', 'type'], 'required'],
            [['pay_needed'], 'required'],
            [
                [
                    'uid',
                    'type',
                    'status',
                    'forbidden',
                    'pay_needed',
                    'created_at',
                    'updated_at',
                    'job_type',
                    'gender_needed',
                    'salary',
                    'salary_type',
                    'pay_type'
                ],
                'integer'
            ],
            [
                [
                    'content',
                    'sign_end',
                    'start_at',
                    'end_at',
                ],
                'string'
            ],
            [
                [
                    'title',
                    'contact_name',
                    'contact_phone',
                    'color',
                    'latitude',
                    'longitude',
                    'ad_code',
                ],
                'string',
                'max' => 255
            ],
//            [
//                ['uid'],
//                'exist',
//                'skipOnError'     => true,
//                'targetClass'     => User::className(),
//                'targetAttribute' => ['uid' => 'id']
//            ],
            [['work_start', 'work_end'], 'safe'],
            ['end_at', 'validateEndTime'],
            ['work_end', 'validateWorkTime'],
        ];
    }

    public function validateEndTime($attribute, $params)
    {
        if ( ! $this->hasErrors()) {
            if ($this->start_at > $this->end_at) {
                $this->addError($attribute, '结束时间不能早于开始时间');
            }
        }
    }

    public function validateWorkTime($attribute, $params)
    {
        if ( ! $this->hasErrors()) {
            if ($this->work_start > $this->work_end) {
                $this->addError($attribute, '下班时间不能早于上班时间');
            }
        }
    }

    public static function getColors()
    {
        $result     = [];
        $colorGoods = Goods::find()->where(['type' => Goods::TYPE_COLORS])->all();
        foreach ($colorGoods as $item) {
            $result[$item->color] = $item->title;
        }

        return $result;
    }

    public function beforeSave($insert)
    {
        $this->type = Job::PARTTIME_TYPE;
        if ($insert) {
            $this->created_at = $this->updated_at = time();
        } else {
            $this->created_at = time();
        }
        $this->uid      = isset($this->uid) ? $this->uid : 1;
        $this->type     = self::PARTTIME_TYPE;
        $this->sign_end = strtotime($this->sign_end);
        $this->start_at = strtotime($this->start_at);
        $this->end_at   = strtotime($this->end_at);

        return parent::beforeSave($insert);
    }

    public function afterSave($insert, $changedAttributes)
    {
        $info = new JobInfo();
//        var_dump($info);
        if ($insert) {
            $info->jid = $this->id;
        } else {
            $info = JobInfo::findOne(['jid' => $this->id]);
            if (empty($info)) {
                $info      = new JobInfo();
                $info->jid = $this->id;
            }
        }

        $info->job_type = empty($this->job_type) ? 1 : $this->job_type;
        if (isset($this->color)) {
            $info->color = $this->color;
        }
        $info->latitude      = $this->latitude;
        $info->longitude     = $this->longitude;
        $info->ad_code       = $this->ad_code;
        $info->gender_needed = $this->gender_needed;
        $info->sign_end      = $this->sign_end;
        $info->start_at      = $this->start_at;
        $info->end_at        = $this->end_at;
        $info->work_end      = $this->work_end;
        $info->work_start    = $this->work_start;
        $info->salary        = $this->salary;
        $info->people_num    = $this->people_num;
        if (isset($this->salary_type)) {
            $info->salary_type = $this->salary_type;
        }
        $info->pay_typed = $this->pay_type;
        $info->good_id   = $this->good_id;
        $info->save(false);
        parent::afterSave($insert, $changedAttributes);
    }

    public function beforeDelete()
    {
        if ($this->jobInfo) {
            $this->jobInfo->delete();
        }
        if ($this->jobRecords) {
            foreach ($this->jobRecords as $job_record) {
                $job_record->delete();
            }
        }

        return parent::beforeDelete(); // TODO: Change the autogenerated stub
    }


}